package com.seeqool.v1;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.swing.JComboBox;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYDotRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class Main {

	public static void main(String[] args) throws ClassNotFoundException, SQLException, InterruptedException {
		
		String username = "SeeQooL_test";
		String password = "testpass";
		
		new MainWindow();

		// Make the Database drivers available
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		Class.forName("com.mysql.jdbc.Driver");

        // set this to a MS Access DB you have on your machine
        String filename = "D:/Test.mdb";
        String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
        database+= filename.trim() + ";DriverID=22;READONLY=true}"; // add on to the end 
        // now we can get the connection from the DriverManager

		
		String query = "SELECT volume as X_Volume, variability as Y_Variability, main_customer_name as G_Customer FROM test2 ORDER BY G_Customer ASC";
		
		String url = "jdbc:mysql://localhost:8306/seeqool_test_db";
		//url = database;
		username = "seeqool";
		password = "seeqool";/**/
		MainWindow.dbQueryManager = new QueryManager(url, username, password);
		
		DatabaseBrowser.browse(url, username, password);

		Connection con = DriverManager.getConnection(url, username, password);
		Statement stmt = con.createStatement();
		
		ResultSet rs=stmt.executeQuery(query);
		
		XYSeriesCollection data=new XYSeriesCollection();
		final XYSeriesCollection highlight=new XYSeriesCollection();
		
		XYDotRenderer BGrenderer = new XYDotRenderer();
		
		BGrenderer.setDotHeight(2);
		BGrenderer.setDotWidth(2);
		BGrenderer.setSeriesPaint(0, Color.LIGHT_GRAY);

		XYDotRenderer FGrenderer = new XYDotRenderer();
		
		FGrenderer.setDotHeight(6);
		FGrenderer.setDotWidth(6);
		FGrenderer.setSeriesPaint(0, Color.red);

		ValueAxis xaxis = new NumberAxis();
		ValueAxis yaxis = new NumberAxis();
		
		XYPlot plot = new XYPlot(highlight, xaxis, yaxis, FGrenderer);
		plot.setRenderer(0, FGrenderer);
		plot.setDataset(1, data);
		plot.setRenderer(1, BGrenderer);
		
		ChartFrame frame=new ChartFrame("SeeQooL Testing", plot);
		frame.pack();
		//frame.setVisible(true);
		
		final Hashtable<String, XYSeries> customers = new Hashtable<String, XYSeries>();

		XYSeries background=new XYSeries("other");
		XYSeries temp=new XYSeries("temp");
		while (rs.next()) {
			if (!customers.containsKey(rs.getString(3))) {
				temp = new XYSeries(rs.getString(3));
				temp.setDescription(rs.getString(3));
				customers.put(rs.getString(3), temp);
			}
			customers.get(rs.getString(3)).add(Math.log(rs.getDouble(1)), Math.log(rs.getDouble(2)));
			background.add(Math.log(rs.getDouble(1)), Math.log(rs.getDouble(2)));
		}
		data.addSeries(background);

		String[] custoptions = new String[customers.values().size()];
		
		Enumeration<String> e = customers.keys();
		
		int idx=0;
		while (e.hasMoreElements()) {
			custoptions[idx]=e.nextElement();
			idx++;
		}
		
		final JComboBox customer = new JComboBox(custoptions);
		
		frame.addItem(customer);
		frame.pack();
		
		customer.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){ 
				for (XYSeries ser : customers.values() ) {
					if (ser.getDescription().equals(customer.getSelectedItem())) {
						highlight.removeAllSeries();
						highlight.addSeries(ser);
					}
				}
			}
	    });		
		
		rs.close();
		stmt.close();
		con.close();
		MainWindow.addLog("Ready...");
	}
}
